package com.test.dababy.dababy_04_home;

import static com.test.dababy.dababy_00_utils.DababyConfigs.PermissionCode_Home;
import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.test.dababy.dababy_00_analytics.DababyUtil.DababyAnalyticsUtil;
import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Customer_dababyServiceDialog;
import com.test.dababy.dababy_06_loan_apply.LoanApplyActivity;
import com.test.dababy.dababy_05_process.Pro_dababycessAct_dababyivity;
import com.test.dababy.dababy_06_loan_apply.LoanApplyActivity_OldUser;
import com.test.dababy.dababy_07_loan_status.BaseLoanFragment;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_05_process.Pro_dababycessIn_dababyfo;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

import pub.devrel.easypermissions.EasyPermissions;

public class HomeFr_dababyagment extends BaseLoanFragment {


    private int jmklsjkr =175;
    private String nljkr4rthio ="";
    private String jnlujser97 ="";

    public void kldrtt7f(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getNljkr4rthio()))
                    setNljkr4rthio(getJmklsjkr() + getJnlujser97());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setJmklsjkr(int jmklsjkr) {
        this.jmklsjkr = jmklsjkr;
    }

    public void setNljkr4rthio(String nljkr4rthio) {
        this.nljkr4rthio = nljkr4rthio;
    }

    public void setJnlujser97(String jnlujser97) {
        this.jnlujser97 = jnlujser97;
    }

    public int getJmklsjkr() {
        return jmklsjkr;
    }

    public String getNljkr4rthio() {
        return nljkr4rthio;
    }

    public String getJnlujser97() {
        return jnlujser97;
    }






    private SwipeRefreshLayout refreshLayout;
    private ImageButton csIb;
    private TextView amountTv;
    private TextView dailyInterestTv;
    private Button loanBtn;
    private TextView viewProcessTv;

    private Runnable processR_dababyunnable;

    private final Runnable applyRunn_dababyable = () -> {

        OkHttpUtil.getInstance_dababy().uploadEventPoint("HOME","APPLY_CLICK","");

        String[] homePermissions = App_dababyShow_dababyCustomize.getInstance_dababy().getHomePermission_dababy();

        if (DababyUtil.checkPermissions_dababy(requireActivity(), homePermissions)) {
            //device info upload
            try {
                if (DababyUtil.didLogin_dababy()) {
                    DababyAnalyticsUtil.INSTANCE.saveDeviceS2SInfo_dababy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            //申请流程
            processR_dababyunnable = () -> {
                //无流程：跳转到申请页
                if (Pro_dababycessIn_dababyfo.getInstance_dababy().getSteps_dababy().isEmpty())
                    toApply();
                //有流程：跳转到流程页
                else startActivity(new Intent(requireContext(), Pro_dababycessAct_dababyivity.class));
            };

            startLoading(requireContext(),"");
            OkHttpUtil.getInstance_dababy().requestUsPrcs(this::onUserProcessRe_dababysponse);

        } else
            EasyPermissions.requestPermissions(requireActivity(), getString(R.string.launch_permissions_all_dababy), PermissionCode_Home, homePermissions);

    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dababy_home, container, false);

        refreshLayout = view.findViewById(R.id.fdh_refreshlayout);
        csIb = view.findViewById(R.id.fdh_ib_cs);
        amountTv = view.findViewById(R.id.fdh_tv_amount);
        dailyInterestTv = view.findViewById(R.id.fdh_tv_daily_interest);
        loanBtn = view.findViewById(R.id.fdh_btn_loan);
        viewProcessTv = view.findViewById(R.id.fdh_tv_view_process);

        refreshLayout.setOnRefreshListener(() -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("HOME","REFRESH_PULL","");
            refreshLayout.setRefreshing(false);
            startLoading(requireContext(),"");
            kldrtt7f();
            OkHttpUtil.getInstance_dababy().requestUsPrcs(this::onUserProcessRe_dababysponse);
        });

        csIb.setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("HOME","CUSTOMER_SERVICE_CLICK","");
            new Customer_dababyServiceDialog(requireContext()).show();
        });

        dailyInterestTv.setText(requireContext().getString(R.string.home_daily_interest_xxx,App_dababyShow_dababyCustomize.getInstance_dababy().getMaxDailyInterest()));

        initVie_dababyw();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("HOME","START","");
        startLoading(requireContext(),"");
        OkHttpUtil.getInstance_dababy().requestHomeAmountList(this::initM_dababysg);
    }

    @Override
    public void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("HOME","END","");
    }


    @SuppressLint("SetTextI18n")
    private void initM_dababysg(){
        stopLoading();
        amountTv.setText(LnLi_dababymitInfo.getInstance_dababy().getLimitAmount_dababy()+" "+ LnLi_dababymitInfo.getInstance_dababy().getCurrency_dababy());
    }

    private void initVie_dababyw(){

        //申请
        loanBtn.setOnClickListener(v -> applyRunn_dababyable.run());

        viewProcessTv.setOnClickListener(v -> {
            processR_dababyunnable = () -> new Proce_dababyssDialog(requireContext(), applyRunn_dababyable).show();
            DababyUtil.startLoading(requireContext(),"");
            OkHttpUtil.getInstance_dababy().requestUsPrcs(this::onUserProcessRe_dababysponse);
        });

    }

    public void onUserProcessRe_dababysponse(){
        String state = Pro_dababycessIn_dababyfo.getInstance_dababy().getDababyUtil();
        if (DababyUtil.isStateChange_dababy(state)) {//刷新用户状态
            stopLoading();
            FragmentActivity activity = requireActivity();
            DababyUtil.clearState_dababy();
            activity.startActivity(new Intent(activity, MainAct_dababyivity.class));
            activity.finish();
        }else if (processR_dababyunnable !=null) {
            stopLoading();
            processR_dababyunnable.run();
            processR_dababyunnable = null;
        }else OkHttpUtil.getInstance_dababy().requestHomeAmountList(this::initM_dababysg);
    }


    public void onPermissionsGranted() {
        applyRunn_dababyable.run();
    }

    //跳转到申请页
    private void toApply(){
        if (Pro_dababycessIn_dababyfo.getInstance_dababy().isNewUser_dababy())
            startActivity(new Intent(requireActivity(), LoanApplyActivity.class));
        else startActivity(new Intent(requireActivity(), LoanApplyActivity_OldUser.class));
    }

}